package dao;

import beans.BoardFile;
import db.DBQuery;
import db.RedisQuery;
import server.whiteboard.WhiteBoardManager;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class BoardFileDAO extends CommonDAO<BoardFile> {

    public final static BoardFileDAO ME = new BoardFileDAO();

    @Override
    public String tableName() {
        return "wb_board_file";
    }

    /**
     * 将Redis缓存中的数据存入MySQL
     * @param board_id
     */
    public void saveBoardContent(long board_id) {
        String key = tableName()+":"+board_id+":getBoardShapeList";
        List<String> shapeList = RedisQuery.getList(key);
        if (shapeList != null) {
            String sql = "update " + tableName() + " set board_content = ? where board_id=" + board_id + " limit 1";
            DBQuery.updateBlob(sql, shapeList);
        }
    }

    public void deleteBoardFile(long board_id) {
        String sql = "delete from " + tableName() + " where board_id=?";
        DBQuery.update(sql, board_id);
    }

    public List<String> getBoardShapeList(long board_id) {
        String key = tableName()+":"+board_id+":getBoardShapeList";
        List<String> shapeList = RedisQuery.getList(key);
        if (shapeList == null) {
            String sql = "select * from " + tableName() + " where board_id=? limit 1";
            shapeList = new ArrayList<>();
            try {
                BoardFile boardFile = DBQuery.read_bean(BoardFile.class, sql, board_id);
                byte[] boardContent = boardFile.getBoard_content();
                if (boardContent != null && boardContent.length > 0) {
                    ByteArrayInputStream byteInt = new ByteArrayInputStream(boardContent);
                    ObjectInputStream objInt = new ObjectInputStream(byteInt);
                    shapeList = (List<String>) objInt.readObject();
                }
                //先清除一下，防止有旧数据
                RedisQuery.deleteByKey(key);
                RedisQuery.pushList(key, shapeList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //刷新过期时间
        RedisQuery.expireKey(key, 3600);
        return shapeList;
    }

    public void addBoardShape(long board_id, List<String> updateShapeList) {
        String key = tableName()+":"+board_id+":getBoardShapeList";
        RedisQuery.pushList(key, updateShapeList);
    }

    public void clearCacheBoard(long board_id) {
        String key = tableName()+":"+board_id+":getBoardShapeList";
        RedisQuery.clearList(key);
    }

    public void addOnlineMember(long boardId, String member, String address) {
        String key1 = tableName()+":"+boardId+":OnlineMember";
        RedisQuery.addSetElem(key1, member);
        String key2 = tableName()+":"+boardId+":MemberAddress";
        RedisQuery.addSetElem(key2, address);
    }

    public void deleteOnlineMember(long boardId, String member, String address) {
        String key1 = tableName()+":"+boardId+":OnlineMember";
        RedisQuery.removeSetElem(key1, member);
        String key2 = tableName()+":"+boardId+":MemberAddress";
        if (getOnlineMembers(boardId).isEmpty())
            RedisQuery.deleteByKey(key2);
        else
            RedisQuery.removeSetElem(key2, address);
    }

    public Set<String> getOnlineMembers(long boardId) {
        String key1 = tableName()+":"+boardId+":OnlineMember";
        return RedisQuery.getSet(key1);
    }

    public Set<String> getOnlineMembersAddress(long boardId) {
        String key1 = tableName()+":"+boardId+":MemberAddress";
        return RedisQuery.getSet(key1);
    }
}
